package com.treehouse.gui.consoleview;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.treehouse.gui.ViewControllerComponent;
import com.treehouse.logging.ILoggingListener;
import com.treehouse.logging.Logger;
import com.treehouse.service.application.IApplicationService;

public class ConsoleViewController extends ViewControllerComponent implements
		IConsoleViewController, ILoggingListener {

	private JTextArea console;
	private JTextField inputConsole;

	public ConsoleViewController(IApplicationService applicationService) {
		super(applicationService);
		this.configConsole();
	}

	private void configConsole() {
		this.console = new JTextArea();
		this.console.setEditable(false);
		this.console.setSize(this.getView().getSize());
		this.console.setMaximumSize(this.getView().getSize());

		Logger.addListener(this);

		JPanel consolePanel = this.getView();
		consolePanel.setLayout(new BorderLayout());
		consolePanel.setBorder(BorderFactory.createTitledBorder("Console"));
		consolePanel.setVisible(true);

		this.console.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		inputConsole = new JTextField();
		inputConsole.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.console.setAutoscrolls(true);
		JScrollPane pane = new JScrollPane(this.console);
		pane.setAutoscrolls(true);
		pane.setPreferredSize(new Dimension(this.getView().getSize().height, this.getView().getSize().width));
		consolePanel.add(pane, BorderLayout.CENTER);
		consolePanel.add(inputConsole, BorderLayout.PAGE_END);

		this.inputConsole.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {

			}

			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					sendCommand();
				}

			}

			@Override
			public void keyReleased(KeyEvent e) {

			}

		});
	}

	private void sendCommand() {
		String command = this.inputConsole.getText();
		this.inputConsole.setText("");
		Logger.inf("Command: " + command);
	}

	@Override
	public void inf(String inf) {
		if (this.console.getText().length() < 2) {
			this.console.setText(this.console.getText() + "[INFO] " + inf);
		} else
			this.console.setText(this.console.getText() + '\n' + "[INFO] "
					+ inf);

		this.console.setCaretPosition(this.console.getDocument().getLength());
	}

	@Override
	public void err(String err) {
		if (this.console.getText().length() < 2) {
			this.console.setText(this.console.getText() + "[ERROR] " + err);
		} else
			this.console.setText(this.console.getText() + '\n' + "[ERROR] "
					+ err);

		this.console.setCaretPosition(this.console.getDocument().getLength());
	}

}
